<script>
import Vue from 'vue';
import { Icon } from 'vant';
import store from 'store2'

Vue.use(Icon);

export default {
  props: {
    keywords: String
  },

  data() {
    return {
      inputValue: this.keywords
    }
  },

  methods: {
    back() {
      this.$router.back()
    },
    search() {
      this.$router.push({
        name: 'list',
        query: {
          keywords: this.inputValue
        }
      })

      // 本地搜索关键字存储
      const lastest = store.get('latestSearches') || []
      if (!lastest.includes(this.inputValue)) {
        lastest.push(this.inputValue)
        store.set('latestSearches', lastest)
      }
    }
  }
}
</script>

<template>
  <div class="searchbar-container">
    <div @click="back">
      <van-icon name="arrow-left" size=".2rem" />
    </div>
    <div>
      <van-icon name="search" size=".2rem" />
      <input type="text" v-model="inputValue">
    </div>
    <div @click="search">搜索</div>
  </div>
</template>

<style lang="scss" scoped>
.searchbar-container {
  height: .44rem;
  background: #ee742f;
  display: flex;
  font-size: .16rem;
  div {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    &:first-child {
      width: .4rem;
    }
    &:nth-child(2) {
      flex: 1;
      position: relative;
      input {
        background: #d55f27;
        border: 0;
        border-radius: .06rem;
        width: 100%;
        height: .3rem;
        color: #fff;
        padding: 0 .1rem 0 .35rem;
      }
      .van-icon-search {
        position: absolute;
        left: .1rem;
        top: .12rem;
      }
    }
    &:last-child {
      width: .6rem;
    }
  }
} 
</style>
